$(function() {
  var listData = {
    "initData": { //默认参数配置
      "page": "1",
      "pageSize": "10"
    },
    "url": site.apiUrl + "/hospitalManager/selectMedicalHospitalInfoList", // 数据请求url
    "type": "get",
    // "checkname": "datachk",
    "col": [ //数据列
      { "field": "coverImg", "display": "商品图片", "fun": "changePic", "width": "1" },
      { "field": "hospitalName", "display": "用户名称" },
      { "field": "doctorNum", "display": "购买商品名称" },
      { "field": "doctorNum", "display": "订单金额" },
      {
        "field": "status",
        "display": "订单状态",
        "show": [
          { "value": "0", "html": "<span>待付款</span>" },
          { "value": "1", "html": "<span>已付款</span>" },
          { "value": "2", "html": "<span>已发货</span>" },
          { "value": "3", "html": "<span>待评价</span>" },
          { "value": "4", "html": "<span>已完成</span>" }
        ]
      },
      { "field": "createTime", "display": "下单日期", "fun": "changeDate", "width": "2" }
    ],
    "option": [ // 操作按钮配置
      { "icon": "view", "tip": "查看物流信息", "url": "viewLogistics.html?orderId={{id}}" },
      { "icon": "ogoto", "tip": "查看评论", "url": "viewComment.html?orderId={{id}}" },
      { "icon": "edit", "tip": "发货", "url": "edit.html?orderId={{id}}" }
    ]
  }
  $("#orderList").getList(listData);

  // 起止日期选择
  laydate.render({
    elem: '#startEndTime',
    theme: 'molv',
    range: '~',
    done: function(value, date, endDate){
      if (value != '') {
        var startTime = value.split(" ~ ")[0];
        var endTime = value.split(" ~ ")[1];
        $("#startTime").val(startTime);
        $("#endTime").val(endTime);
      }
    }
  });

  // 导出表格
  $("body").on("click", "#export_excel", function() {
    exportExcelWithJS();
  })

  // 重置按钮
  $("body").on("click", "#reset", function() {
    $("#startTime").val("");
    $("#endTime").val("");
  })

  //批量删除
  $("#batchDelBtn").click(function() {
    var c = "";
    $("input[name=datachk]:checked").each(function() {
      c += $(this).val() + ',';
    });
    if (c != null && c != '') {
      deleteBrand(c);
    } else {
      ly.toast("请选择删除项");
    }
  });

});

// 设置默认图片
function changePic(v, o, d) {
  if (d.coverImg != null && d.coverImg != '') {
    $(o).html("<img src=" + d.coverImg + " width='30' height='30'>");
  } else {
    $(o).html("<img src='" + site.interUrl + "/img/default_head.jpg' width='30' height='30'>");
  }
}

// 时间戳转换时间
function changeDate(v, o, d) {
  var baseDateTime;
  var timestamp = new Date(d.createTime);
  baseDateTime = timestamp.toLocaleDateString().replace(/\//g, "-") + " " + timestamp.toTimeString().substr(0, 8)
  if (d.createTime != null && d.createTime != '') {
    $(o).html(baseDateTime)
  }
}

// 跳转用户管理列表
function goUserPage(v, o, d) {
  $(o).html('<a href="'+ site.interUrl +'pages/doctorManage/list.html?groupid=2&id='+ d.id +'" title="">'+ d.doctorNum +'</a>');
}

// 导出EXCEL
function exportExcelWithJS(){
  // 表格数据是否为空
  var tableIsEmpty = $('#orderList table tr td').hasClass("nodata");
  if (!tableIsEmpty) {
    ly.confirm("确定导出吗？", function(r) {
      $('#orderList table').tableExport({
        type:'excel',
        fileName:'订单列表',
      });
    })
  } else {
    ly.toast("数据为空")
  }
}

//删除
function deleteBrand(id) {
  ly.confirm("确定删除吗？", function(r) {
    var url = site.apiUrl + "/hospitalManager/deleteMedicalPopular";
    request({
      url: url,
      data: { "id": id },
      type: "post",
      callback: function(f) {
        if (f.code == 200) {
          ly.toast("删除成功");
          $("#orderList").getList.refresh(); //刷新当前分页
        } else {
          ly.toast(f.desc || f.msg);
        }
      }
    })
  })
}